package com.meilai.project.mapper.business.contract;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.business.contract.ContractApplyAmendListDTO;
import com.meilai.project.dto.business.contract.ContractApplyLatestListDTO;
import com.meilai.project.dto.business.contract.ContractApplyListDTO;
import com.meilai.project.entity.business.contract.ContractApply;
import com.meilai.project.entity.business.contract.ContractNotCompleteChooseAndDateRangePO;
import com.meilai.project.vo.business.contract.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;

/**
 * @author xchen
 * @since 2022-03-02
 */
@Mapper
public interface ContractApplyMapper extends BaseMapper<ContractApply> {

    @Select(" select DISTINCT mpos.choose_id,mpos.daterange_id " +
            " from media_point_occupy_snapshot mpos " +
            " join media_point_choose mpc on mpos.choose_id = mpc.id " +
            " where mpc.type = 2 ")
    List<ContractNotCompleteChooseAndDateRangePO> listNotCompleteContractChooseAndDateRange();

    List<ContractApplyVO> listByChooseIdList(@Param("userId") Long userId, @Param("chooseIdList") List<Long> chooseIdList);

    @Select("select ca.*,ci.name as customer_name,ci.maintenance_staff_id from contract_apply ca left join customer_info ci on ca.customer_id=ci.id where ca.id = #{id}")
    ContractApplyVO queryDetailById(@Param("id") Long id);

    List<ContractApplyVO> queryDetailByIds(@Param("ids") List<Long> ids);

    List<ContractApplyVO> listByRootId(@Param("root_id") Long root_id);

    List<ContractApplyVO> listByParentId(@Param("parent_id") Long parent_id);

    List<ContractApplyVO> listForMarketByCustomerId(
            @Param("customerId") Long customerId,
            @Param("mustIncludeContractId") Long mustIncludeContractId);

    List<ContractApplyForADMakeVO> listForADMakeByDeptId(
            @Param("customer_id") Long customer_id,
            @Param("mustIncludeContractId") Long mustIncludeContractId);

    List<ContractApplyVO> listLatestByCustomerId(@Param("customerId") Long customerId);

    List<ContractApplyVO> listLatestChild(@Param("ids") List<Long> contractIds);

    List<ContractApplyVO> listLatest(@Param("ids") List<Long> contractIds);

    List<ContractApplyVO> listPageLatest(Page<ContractApplyVO> page,
                                         @Param("query") ContractApplyLatestListDTO dto);

    List<ContractApplyVO> selectPageList(Page<ContractApplyVO> page,
                                         @Param("query") ContractApplyListDTO dto);

    List<ContractChildCountVO> listSubCount(@Param("ids") List<Long> contractIds);

    List<ContractChildCountVO> listDirectSubCount(@Param("ids") List<Long> contractIds);


    List<ContractApplyVO> selectPageAmendList(Page<ContractApplyVO> page,
                                              @Param("query") ContractApplyAmendListDTO dto);


    List<String> listPartAHistory(@Param("customer_id") Long userId);

    void updateMode(@Param("chooseIdList") List<Long> chooseIdList, @Param("hangUp") int hangUp);

    List<ContractApply> queryEffectiveContractList(@Param("begin_date") LocalDate begin_date, @Param("end_date") LocalDate end_date, @Param("ids") List<Long> ids);

    List<ContractApplyVO> list_by_customer_id(Long customerId);

    List<ContractMediaPrice> queryMediaDetailBysIds(@Param("ids") Set<Long> ids);

    List<ContractApply> query_contract_list_by_reimburse(@Param("user_id") Long user_id);


    /**
     * 获取所有的有效的合同
     *
     * @return
     */
    List<ContractApply> queryValidContract();

    /**
     * 查询合同对应的资源类型和数量
     * @param contract_ids 合同ids
     */
    List<ContractMediaCountDTO> getMediaTypeCount(@Param("contract_ids") List<Long> contract_ids);
}
